package com.example.finalwork.Pay;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.finalwork.DAO.PayDAO;
import com.example.finalwork.Entity.Pay;
import com.example.finalwork.LoginActivity;
import com.example.finalwork.R;
import com.example.finalwork.lmpl.PayDAOlmpl;

import java.util.List;

import static org.junit.Assert.assertNotEquals;

public class PayDelActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "PayDelActivity";
    private TextView paydel_id,paydel_name,paydel_date,paydel_amount,paydel_remark;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paydel);

        paydel_id = findViewById(R.id.paydel_id);
        paydel_name = findViewById(R.id.paydel_name);
        paydel_amount = findViewById(R.id.paydel_amount);
        paydel_date = findViewById(R.id.paydel_date);
        paydel_remark = findViewById(R.id.paydel_remark);



        findViewById(R.id.pay_del_query).setOnClickListener(this);
        findViewById(R.id.pay_del_button).setOnClickListener(this);
        findViewById(R.id.pay_del_exit).setOnClickListener(this);

        setTitle("删除支出项目功能");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pay_del_query:
                PayDAO payDAO = new PayDAOlmpl(this);
                List<Pay> pays = payDAO.GetAllPay();
                for(int i = 0 ; i < pays.size() ; i++)
                {
                    Pay pay = pays.get(i);
                    if(pay.getPay_Id() == Integer.parseInt(paydel_id.getText().toString()) && pay.getPay_User().equals(LoginActivity.data))
                    {
                        paydel_name.setText(pay.getPay_Name());
                        paydel_date.setText(pay.getPay_Date());
                        paydel_amount.setText(pay.getPay_Amount());
                        paydel_remark.setText(pay.getPay_Remark());
                        Toast.makeText(this,"查询成功！",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                Toast.makeText(this,"无查询结果！",Toast.LENGTH_SHORT).show();
                break;
            case R.id.pay_del_button:
                PayDAO payDAO2 = new PayDAOlmpl(this);
                int result = payDAO2.delPayById(Integer.parseInt(paydel_id.getText().toString()));
                if(result ==0)
                {
                    Toast.makeText(this,"删除失败",Toast.LENGTH_SHORT).show();
                    break;
                }
                Log.d(TAG,"del user result - >" + result);
                assertNotEquals(-1,result);
                Toast.makeText(this,"删除成功，返回上一级",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, PayMainActivity.class));
                break;
            case R.id.pay_del_exit:
                Toast.makeText(this,"返回上一级",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this,PayMainActivity.class));
                break;
        }
    }
}
